#include "Member.h"
#include "MemberDAO.h"

namespace everest
{
namespace sample
{

const char *MemberDAO::s_insertStmt = "insert into test.member (id,username,password,creat_date) values ( :0<int>, :1<char[32]>, :2<char[32]>, :3<timestamp>)";
const char *MemberDAO::s_selectStmt = "select id,username,password,creat_date from test.member ";
const char *MemberDAO::s_selectStmtById = "select username,password,creat_date from test.member where id= :0<int>";
const char *MemberDAO::s_updateStmtById = "update test.member set  username = :0<char[32]>, password = :1<char[32]>, creat_date = :2<timestamp> where id= :3<int>";
const char *MemberDAO::s_deleteStmtById = "delete from test.member where id= :0<int>";

MemberDAO::MemberDAO(otl_connect *pconn, size_t bufsize)
	: DataAccessObject(pconn, bufsize), m_hasInsert(false), m_hasUpdate(false), m_hasDelete(false){}

MemberDAO::~MemberDAO(){ this->clear(); }

void MemberDAO::clear() {
	if (m_hasInsert) this->m_insertStream.close();
	if (m_hasUpdate) this->m_updateStream.close();
	if (m_hasDelete) this->m_deleteStream.close();
	this->m_hasInsert = false;
	this->m_hasUpdate = false;
	this->m_hasDelete = false;
}

int  MemberDAO::flush() {
	if ( m_hasInsert ) m_insertStream.flush();
	if ( m_hasUpdate ) m_updateStream.flush();
	if ( m_hasDelete ) m_deleteStream.flush();
}

int MemberDAO::insert(const Member& obj) {
	if ( !m_hasInsert ) m_insertStream.open(m_bufsize, s_insertStmt, *m_conn);
	m_insertStream<<obj.getId()<<obj.getUserName()<<obj.getPassword()<<convertToDateTime(obj.getCreateDate());
	m_hasInsert = true;
	return 1;
}

int MemberDAO::update(const Member& obj) {
	if ( !m_hasUpdate ) m_updateStream.open(m_bufsize, s_updateStmtById, *m_conn);
	m_updateStream<<obj.getUserName()<<obj.getPassword()<<convertToDateTime(obj.getCreateDate())<<obj.getId();
	m_hasUpdate = true;
	return 1;
}

int MemberDAO::remove(const Member& obj) {
	if ( !m_hasDelete ) m_deleteStream.open(m_bufsize, s_deleteStmtById, *m_conn);
	m_deleteStream<<obj.getId();
	m_hasDelete = true;
	return 1;
}

int MemberDAO::select(Member& obj) {
	int result = 0;
	otl_nocommit_stream os(m_bufsize, s_selectStmtById, *m_conn);
	os<<obj.getId();
	if (!os.eof() ) { 
		obj.setUserName(getStringFromStream(os));
obj.setPassword(getStringFromStream(os));
obj.setCreateDate(convertToString(getDateTimeFromStream(os)));
		result = 1; }
 else {
		 result = 0;
	}
	return result;
}

int MemberDAO::select(std::vector<Member>& vec, const char *pszWhere) {
	std::string sql;
	sql.reserve(1024);
	sql.assign(s_selectStmt);
	if (pszWhere && pszWhere[0] != '\0') {
		sql.append(" where ").append(pszWhere);
	}	int result = 0;
	otl_nocommit_stream os(m_bufsize, sql.c_str(), *m_conn);
	while (!os.eof()) {
		vec.push_back(Member());
		Member& obj = vec.back();
		obj.setId(getIntegerFromStream(os));
obj.setUserName(getStringFromStream(os));
obj.setPassword(getStringFromStream(os));
obj.setCreateDate(convertToString(getDateTimeFromStream(os)));
		result += 1;
		}
	return result;
}
}
}// END OF NAMESPACE everest::sample


